﻿namespace PowerSong.UserInterface
    {
    using System.Windows;
    using System.Linq;
    using System;

    /// <summary>
    /// Interaction logic for EditStylesWindow.xaml
    /// </summary>
    public partial class EditStylesWindow : Window
        {

        public EditStylesWindow()
            {
            InitializeComponent();
            UpdateLists();
            }

        private void CreateStyleButton_Click(object sender, RoutedEventArgs e)
            {

            // Ask for basic style details
            var window = new AddStyleWindow();
            if (window.ShowDialog() == true)
                {
                var styleCreated = window.StyleCreated;
                UpdateLists();

                // Get more details
                var detailedWindow = new EditStyleWindow(styleCreated);
                if (detailedWindow.ShowDialog() == true)
                    {
                    UpdateLists();
                    if (State.Database.Styles.Count() == 1) DefaultStyleComboBox.SelectedIndex = 0;
                    }

                }

            }

        private void StylesListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
            {
            EditStyleButton.IsEnabled = StylesListBox.SelectedItems.Count > 0;
            DeleteStyleButton.IsEnabled = StylesListBox.SelectedItems.Count > 0;
            }

        private void DeleteStyleButton_Click(object sender, RoutedEventArgs e)
            {
            if (Utilities.Confirm("Delete Styles", "Are you sure you wish to delete all of the selected styles?"))
                {
                var defaultStyle = State.Settings.DefaultStyle;
                foreach (SongDatabase.Style style in StylesListBox.SelectedItems)
                    {

                    // Is the default style being deleted?
                    if (style == defaultStyle)
                        {
                        State.Settings.DefaultStyle = null;
                        State.Settings.SaveChanges();
                        }

                    // Are there songs which depend on this style?
                    var songCount = 0;
                    foreach (var song in State.Database.Songs.GetAllSongs())
                        {
                        if (song.DefaultStyle != null && song.DefaultStyle.ID == style.ID) songCount += 1;
                        }

                    // Ask for confirmation, if there are songs that depend on this style
                    var proceed = true;
                    if (songCount > 0)
                        {
                        var message = string.Format("There are songs in the database that have been explicity set to use style '{0}' as their default style. If you proceed, then those songs will no longer be assigned a default style." + Environment.NewLine + Environment.NewLine +
                                                    "There {1} currently {2} {3} in the database that use this style explicitly." + Environment.NewLine + Environment.NewLine +
                                                    "Are you sure you wish to delete the style '{4}'?",
                                                    style.Name,
                                                    songCount == 1 ? "is" : "are",
                                                    songCount,                                                    
                                                    songCount == 1 ? "song" : "songs",
                                                    style.Name);
                        proceed = Utilities.Confirm("Delete Style", message);
                        }

                    // Delete the style
                    if (proceed) State.Database.Styles.Delete(style);

                    }
                UpdateLists();
                }
            }

        private void EditStyleButton_Click(object sender, RoutedEventArgs e)
            {
            var selectedStyle = (SongDatabase.Style)StylesListBox.SelectedItem;
            if (selectedStyle != null)
                {
                var window = new EditStyleWindow(selectedStyle);
                if (window.ShowDialog() == true)
                    {
                    UpdateLists();
                    }
                }
            }

        private void UpdateLists()
            {

            var allStyles = State.Database.Styles.GetAllStyles().OrderBy(style => style.Name);

            // Populate styles list
            StylesListBox.Items.Clear();
            foreach (var style in allStyles) StylesListBox.Items.Add(style);

            // Populate default style combo box
            DefaultStyleComboBox.Items.Clear();
            foreach (var style in allStyles)
                {
                DefaultStyleComboBox.Items.Add(style);
                }
            DefaultStyleComboBox.SelectedItem = State.Settings.DefaultStyle;

            }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {

            // Set the default style
            State.Settings.DefaultStyle = (SongDatabase.Style)DefaultStyleComboBox.SelectedItem;
            State.Settings.SaveChanges();

            // A default style must be selected
            if (State.Settings.DefaultStyle == null)
                {
                Utilities.ShowErrorMessage("Cannot Close Window", "Please select a default style. If no styles are defined then please create a style and set it as the default style.");
                e.Cancel = true;
                }

            }

        }

    }
